package com.lg.erp.controller.jml;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.jml.TDepartment;
import com.lg.erp.entity.jml.TPost;
import com.lg.erp.entity.jml.TStaff;
import com.lg.erp.service.jml.ITDepartmentService;
import com.lg.erp.service.jml.ITPostService;
import com.lg.erp.service.jml.ITStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class TStaffController {

    @Autowired
    private ITStaffService staffService;
    @Autowired
    private ITDepartmentService departmentService;
    @Autowired
    private ITPostService postService;

    //模糊查询
    @GetMapping("/staff/{current}/{name}/{dId}")
    public JsonResult getStaff(@PathVariable Integer current,@PathVariable String name,@PathVariable String dId){
        List<TStaff> tStaffs = staffService.selectStaff(current, name, dId);
        for (TStaff staff : tStaffs) {
            TDepartment tDepartment = departmentService.getById(staff.getDId());
            staff.setDName(tDepartment.getName());
            TPost post = postService.getById(staff.getPId());
            staff.setPName(post.getName());
        }
        return new JsonResult(1,"ok",tStaffs);
    }

    //分页查询
    @GetMapping("/staff/{current}")
    public JsonResult getStaffByPage(@PathVariable Integer current){
        Page<TStaff> page=new Page<>(current,10);
        IPage<TStaff> pages = staffService.selectStaffByPage(page);
        return new JsonResult(1,"ok",pages);
    }

    //查询单条数据
    @GetMapping("/staffs/{id}")
    public JsonResult getStaffById(@PathVariable String id){
        TStaff byId = staffService.getById(id);
        TPost post = postService.getById(byId.getPId());
        byId.setPName(post.getName());
        return new JsonResult(1,"ok",byId);
    }

    //查询所有的账号
    @GetMapping("/staff")
    public JsonResult getAccount(){
        List<TStaff> tStaffs = staffService.selectAll();
        return new JsonResult(1,"ok",tStaffs);
    }
    //查询所有员工状态
    @GetMapping("/staffs")
    public JsonResult getStatus(){
        List<TStaff> tStaffs = staffService.selectAllStatus();
        return new JsonResult(1,"ok",tStaffs);
    }

    //修改
    @PutMapping("/staff")
    public JsonResult updateStaff(@RequestBody TStaff staff){
        staff.setPId(staff.getPName());
        staffService.updateById(staff);
        return new JsonResult(1,"ok",null);
    }
    //增加
    @PostMapping("/staff")
    public JsonResult addStaff(@RequestBody TStaff staff){
        staff.setId(String.valueOf(Math.random()));
        staff.setAccountStatus("1");
        staff.setCId("1");
        staffService.save(staff);
        return new JsonResult(1,"ok",null);
    }
    //修改账号状态
    @DeleteMapping("/staff/{current}/{id}")
    public JsonResult deleteStaff(@PathVariable Integer current,@PathVariable String id){
        TStaff byId = staffService.getById(id);
        if (byId.getAccountStatus().equals("1")){
            byId.setAccountStatus("0");
            staffService.update(byId.getAccountStatus(),byId.getId());
            Page<TStaff> page=new Page<>(current,10);
            IPage<TStaff> pages = staffService.selectStaffByPage(page);
            return new JsonResult(1,"ok",pages);
        }
        if (byId.getAccountStatus().equals("0")) {
            byId.setAccountStatus("1");
            staffService.update(byId.getAccountStatus(),byId.getId());
            Page<TStaff> page = new Page<>(current, 10);
            IPage<TStaff> pages = staffService.selectStaffByPage(page);
            return new JsonResult(1, "ok", pages);
        }
        return new JsonResult(1, "ok", null);
    }
}
